<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
// load base class if needed
require_once( APPPATH . 'controllers/base/OperatorBase.php' );

class pengadaan extends ApplicationBase {

    // constructor
    public function __construct() {
        parent::__construct();
        // load model
        $this->load->model('m_pengadaan');
        $this->load->model('m_bagian');
        $this->load->model('m_lokasi');
        $this->load->model('m_barang');
        $this->load->model('m_group_barang');
        $this->load->model('m_kategoribarang');
        $this->load->model('m_jenisbarang');
        $this->load->model('m_merk');
        $this->load->model('m_pengajuan_barang');
        $this->load->model('m_operator_mini');

        $this->load->model('m_ruang');
        $this->load->model('m_vendor');
        // load library
        $this->load->library('tnotification');
        $this->load->library('pagination');
        $this->load->library('datetimemanipulation');
        $this->smarty->assign("dtm", $this->datetimemanipulation);
        // load helpers
        $this->smarty->load_javascript("resource/js/jquery/jquery-ui-1.9.2.custom.min.js");
        $this->smarty->load_style("jquery.ui/redmond/jquery-ui-1.8.13.custom.css");
        // lightbox
        $this->smarty->load_javascript("resource/js/lightbox/lightbox-2.6.min.js");
        $this->smarty->load_style("lightbox/css/lightbox.css");
        // autonumeric
        $this->smarty->load_javascript("resource/js/autonumeric/autonumeric.js");

        //select 2
        $this->smarty->load_javascript("resource/js/select2/select2.js");
        $this->smarty->load_style("select2/select2.css");
    }

    // welcome administrator
    public function index() {
        $this->_set_page_rule("R");

        // set template content
        $this->smarty->assign("template_content", "inventarisasi/pengadaan/list.html");

        //cari
        $search = $this->tsession->userdata('data_search');
        $bagian_cd = empty($search['bagian_cd']) ? '%' : $search['bagian_cd'];
        $tahun = empty($search['tahun']) ? '%' : $search['tahun'];
        $bulan = empty($search['bulan']) ? '%' : $search['bulan'];
        //$tgl = $tahun . "-" . $bulan;
        $params = array($bagian_cd, $tahun, $bulan);

        //sent to display
        $this->smarty->assign("ses_bagian_cd", $bagian_cd);
        $this->smarty->assign("ses_bulan", $bulan);
        $this->smarty->assign("ses_tahun", $tahun);

        /* start of pagination --------------------- */
        // pagination
        $config['base_url'] = site_url("inventarisasi/pengadaan/index/");
        $config['total_rows'] = $this->m_pengadaan->get_count_all_pengadaan_search($params);
        $config['uri_segment'] = 4;
        $config['per_page'] = 10;
        $this->pagination->initialize($config);
        $pagination['data'] = $this->pagination->create_links();
        // pagination attribute
        $start = $this->uri->segment(4, 0) + 1;
        $end = $this->uri->segment(4, 0) + $config['per_page'];
        $end = (($end > $config['total_rows']) ? $config['total_rows'] : $end);
        $pagination['start'] = ($config['total_rows'] == 0) ? 0 : $start;
        $pagination['end'] = $end;
        $pagination['total'] = $config['total_rows'];
        // pagination assign value
        $this->smarty->assign("pagination", $pagination);
        $this->smarty->assign("no", $start);
        /* end of pagination ---------------------- */

        // get data
        $params = array($bagian_cd, $tahun, $bulan, ($start - 1), $config['per_page']);
        $result = $this->m_pengadaan->get_all_pengadaan_pagination_search($params);
        $this->smarty->assign("rs_id", $result);


        $rs_bagian = $this->m_bagian->get_all_bagian();
        $this->smarty->assign("rs_bagian", $rs_bagian);


        $bulan = Array(
            Array("kode" => "01", "name" => "Januari"),
            Array("kode" => "02", "name" => "Februari"),
            Array("kode" => "03", "name" => "Maret"),
            Array("kode" => "04", "name" => "April"),
            Array("kode" => "05", "name" => "Mei"),
            Array("kode" => "06", "name" => "Juni"),
            Array("kode" => "07", "name" => "Juli"),
            Array("kode" => "08", "name" => "Agustus"),
            Array("kode" => "09", "name" => "September"),
            Array("kode" => "10", "name" => "Oktober"),
            Array("kode" => "11", "name" => "November"),
            Array("kode" => "12", "name" => "Desember")
        );

        $this->smarty->assign("bulan", $bulan);

        $tahun = Array(
            Array("tahun" => "2012"),
            Array("tahun" => "2013"),
            Array("tahun" => "2014"),
            Array("tahun" => "2015"),
            Array("tahun" => "2016"),
            Array("tahun" => "2017"),
            Array("tahun" => "2018"),
            Array("tahun" => "2019"),
            Array("tahun" => "2020")
        );
        $this->smarty->assign("tahun", $tahun);

        /*
          $thn_sekarang = date('Y');
          $this->smarty->assign("thn_sekarang", $thn_sekarang);
          $bln_sekarang = date('m');
          $this->smarty->assign("bln_sekarang", $bln_sekarang);
         */

        //image 
        $filepath = base_url() . 'resource/userfile/pengadaan/';
        $this->smarty->assign("filepath", $filepath);

        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();

        // output
        parent::display();
    }

    // proses pencarian
    public function proses_cari() {
        // set page rules
        $this->_set_page_rule("R");
        // data
        if ($this->input->post('save') == "Reset") {
            $this->tsession->unset_userdata('data_search');
        } else {
            $params = array(
                "bagian_cd" => $this->input->post("bagian_cd"),
                "bulan" => $this->input->post("bulan"),
                "tahun" => $this->input->post("tahun")
            );
            $this->tsession->set_userdata("data_search", $params);
        }
        // redirect
        redirect("inventarisasi/pengadaan");
    }

//    public function procces() {
//        //cek data
//        $this->tnotification->set_rules('pengadaan_id', 'ID Pengadaan', 'trim|required');
//
//        $result = $this->m_pengajuan_barang->get_all_barang_by_pengadaan_id($this->input->post('pengadaan_id'));
//
//        if (empty($result)) {
//            $this->tnotification->sent_notification("error", "Data Barang masih kosong");
//            redirect('inventarisasi/pengadaan/pengajuan_barang/' . $this->input->post('pengadaan_id'));
//        } else {
//            //insert data ke barang
//            foreach ($result as $row) {
//                $lokasi_cd = $row['lokasi_cd'];
//                $kategoribarang_cd = $row['kategoribarang_cd'];
//                $jenisbarang_cd = $row['jenisbarang_cd'];
//                $merk_cd = $row['merk_cd'];
//
//                $tgl = date("dmy");
//                $no_barang = 1;
//                $qty = $row['pengajuanbarang_qty'];
//                $penyusutan = $row['pengajuanbarang_penyusutan'] * 12;
//
//                for ($i = 1; $i <= $qty; $i++) {
//                    $no_inventaris = '';
//                    // jika barang generate nomor inventaris 
//                    if ($row['pengajuanbarang_generate_noiventaris'] == '1' && $row['gpbarang_cd'] == '2') {
//                        $no_inventaris = $lokasi_cd . "." . $kategoribarang_cd . "." . $jenisbarang_cd . "." . $merk_cd . "." . $tgl . "." . $i . "." . $qty . "." . $penyusutan;
//                    }
//                    $params = array(
//                        'barang_noinventaris' => $no_inventaris,
//                        'barang_nama' => $row['pengajuanbarang_nama'],
//                        'barang_harga_beli' => $row['pengajuanbarang_hargasatuan'],
//                        'barang_nilai_buku' => $row['pengajuanbarang_hargasatuan'],
//                        'barang_umurekonomis' => $row['pengajuanbarang_penyusutan'],
//                        'barang_keterangan' => $row['pengajuanbarang_keterangan'],
//                        'barang_kondisi' => $row['pengajuanbarang_barang_kondisi'],
//                        'barang_status' => $row['pengajuanbarang_status'], 
//                        'barang_tglbeli' => date('Y-m-d'), 
//                        'barang_tglpenyusutan' => null,
//                        'barang_garansi' => $row['pengajuanbarang_garansi'],
//                        'barang_tgl_garansi' => $row['pengajuanbarang_tgl_garansi'],
//                        'barang_pengguna' => null,
//                        'barang_susut' => $row['pengajuanbarang_susut'],
//                        'gpbarang_cd' => $row['gpbarang_cd'],
//                        'vendor_cd' => $row['vendor_cd'],
//                        'lokasi_cd' => $row['lokasi_cd'],
//                        'ruang_cd' => $row['ruang_cd'],
//                        'kategoribarang_cd' => $row['kategoribarang_cd'],
//                        'jenisbarang_cd' => $row['jenisbarang_cd'],
//                        'merk_cd' => $row['merk_cd'],
//                        'pengadaan_id' => $row['pengadaan_id'],
//                        'mdb' => $this->com_user['user_id']
//                    );
//
//                    $this->m_barang->insert($params);
//                }
//            }
//            $this->m_pengadaan->update_pengadaan_status_generate_by_pengadaan_id(array('1', $this->input->post('pengadaan_id')));
//            //proses
//            if ($this->tnotification->run() !== FALSE) {
//                // insert
//                $this->tnotification->sent_notification("success", "Data berhasil disimpan");
//            }
//
//            redirect('inventarisasi/pengadaan/');
//        }
//    }

    // import data via excel
    public function import() {
        $this->_set_page_rule("R");

        // set template content
        $this->smarty->assign("template_content", "inventarisasi/pengadaan/import.html");


        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();

        // output
        parent::display();
    }

    public function get_id() {
        $time = microtime(true);
        $id = str_replace('.', '', $time);
        return $id;
    }

    public function import_process() {
        // set page rules
        $this->_set_page_rule("C");

        // load
        $this->load->library('phpexcel');
        $this->load->library('tupload');

        $time = microtime(true);
        $id = str_replace('.', '', $time);

        // upload config
        $config['upload_path'] = 'resource/doc/temp/';
        $config['allowed_types'] = 'xls|xlsx';
        $config['file_name'] = "import_" . $id;
        $config['overwrite'] = TRUE;
        $this->tupload->initialize($config);
        if (!$this->tupload->do_upload('import_att')) {
            // jika gagal (kembalikan pesan)
            $this->tnotification->set_error_message($this->tupload->display_errors());
            $this->tnotification->sent_notification("error", "Data gagal disimpan");
        } else {
            $data = $this->tupload->data();
            // read data
            $file_path = 'resource/doc/temp/' . $data['file_name'];
            if (is_file($file_path)) {
                // ready to read
                $worksheet = PHPExcel_IOFactory::load($file_path);
                $sheet = $worksheet->getSheet(0);

                // init
                $i = 6;
                $cell_data = $sheet->getCell('A' . $i);
                $data = $cell_data->getValue();

                $tot_data = 0;
                $tot_insert = 0;
                while (!empty($data)) {
                    $tot_data++;
                    $gpbarang_cd = $this->m_kategoribarang->get_gpbarang_cd_by_kategoribarang_cd($sheet->getCell('J' . $i)->getValue());
                    $params = array(
                        $sheet->getCell('B' . $i)->getValue(),
                        $sheet->getCell('C' . $i)->getValue(),
                        $sheet->getCell('D' . $i)->getValue(),
                        $sheet->getCell('E' . $i)->getValue(),
                        $sheet->getCell('N' . $i)->getValue(),
                        $sheet->getCell('H' . $i)->getValue(),
                        $sheet->getCell('I' . $i)->getValue(),
                        null,
                        $gpbarang_cd,
                        $sheet->getCell('J' . $i)->getValue(),
                        $sheet->getCell('K' . $i)->getValue(),
                        $sheet->getCell('L' . $i)->getValue(),
                        null,
                        $sheet->getCell('M' . $i)->getValue(),
                        "1313"
                    );

                    $result = $this->m_barang->get_barang_by_noinventaris($sheet->getCell('B' . $i)->getValue());
                    //insert                    
                    if (empty($result)) {
                        $tot_insert++;
                        $this->m_barang->insert($params);
                    }

                    // get
                    $i++;
                    $cell_data = $sheet->getCell('A' . $i);
                    $data = $cell_data->getValue();
                }
                // delete after reading
                unlink($file_path);
            }
            // jika sukses
            $this->tnotification->sent_notification("success", $tot_insert . " Data berhasil diimport " . "dari " . $tot_data);
        }

        // default redirect
        redirect("inventarisasi/pengadaan/import");
    }

    // welcome administrator
    public function edit($id) {
        $this->_set_page_rule("R");

        // set template content 
        $this->smarty->assign("template_content", "inventarisasi/pengadaan/edit.html");

        //data
        $result = $this->m_pengadaan->get_pengadaan_by_pengadaan_id($id);
        $this->smarty->assign("result", $result);
        $this->smarty->assign("pengadaan_id", $id);

        $rs_bagian = $this->m_bagian->get_all_bagian();
        $this->smarty->assign("rs_bagian", $rs_bagian);

        //image 
        $filepath = base_url() . 'resource/userfile/pengadaan/';
        $this->smarty->assign("filepath", $filepath);

        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    public function edit_process() {
        //cek data
        $this->tnotification->set_rules('pengadaan_id', 'ID Pengadaan', 'trim|required');
        $this->tnotification->set_rules('pengadaan_nomor', 'Nomor Pengadaan', 'trim|required');
        $this->tnotification->set_rules('pengadaan_date', 'Tanggal Pengadaan', 'trim|required');
        $this->tnotification->set_rules('bagian_cd', 'Bagian', 'trim|required');

        //proses
        if ($this->tnotification->run() !== FALSE) {
            // validasi 
            if (!empty($_FILES['pengadaan_att_surat']['tmp_name'])) {
                // load
                $this->load->library('tupload'); 
                $nomor_pengadaan = $this->input->post('pengadaan_nomor');
                $nomor_pengadaan = str_replace(".", "",$nomor_pengadaan);
                $nomor_pengadaan = str_replace("-", "",$nomor_pengadaan);
                $nomor_pengadaan = str_replace("/", "_",$nomor_pengadaan);

                // delete
                $filename = $nomor_pengadaan;
                // $filepath = 'resource/doc/images/pengadaan/' . $filename;
                $filepath = 'resource/userfile/pengadaan/' . $filename;

                
                // upload config
                $config['upload_path'] = 'resource/userfile/pengadaan/';
                $config['allowed_types'] = 'gif|jpg|png';
                $config['file_name'] = $filename;
                $this->tupload->initialize($config);
                // process upload images
                if ($this->tupload->do_upload_image('pengadaan_att_surat', false, 1000)) {
                    $data = $this->tupload->data();
                    $filename = $data['file_name'];
                    // Hapus file lama
                    $result = $this->m_pengadaan->get_pengadaan_by_pengadaan_id($this->input->post('pengadaan_id'));
                    if ($result['pengadaan_att_surat']!='') {
                        unlink('resource/userfile/pengadaan/' . $filename);
                    }
                    $this->tnotification->sent_notification("success", "File berhasil diupload");
                } else {
                    $filename = NULL;
                    // jika gagal
                    $this->tnotification->set_error_message($this->tupload->display_errors());
                    $this->tnotification->sent_notification("error", "File gagal diupload");
                }
            }
            // parameter
            $params = array(
                $this->input->post('pengadaan_nomor'),
                $this->input->post('pengadaan_date'),
                $this->input->post('bagian_cd'),
                $this->input->post('pengadaan_keterangan'),
                $this->input->post('pengadaan_att_surat'), 
                $this->com_user['user_id'],
                $this->input->post('pengadaan_id')
            );
 
            // insert
            if ($this->m_pengadaan->edit($params)) {  
                // notification
                $this->tnotification->delete_last_field();
                $this->tnotification->sent_notification("success", "Data berhasil disimpan");
            } else {
                // default error
                $this->tnotification->sent_notification("error", "Data gagal disimpan");
            }
        } 
        redirect('inventarisasi/pengadaan/pengajuan_barang/' . $this->input->post('pengadaan_id'));
    }

    // welcome administrator
    public function delete($id) {
        $this->_set_page_rule("R");

        // set template content 
        $this->smarty->assign("template_content", "inventarisasi/pengadaan/delete.html");

        //data
        $result = $this->m_pengadaan->get_pengadaan_by_pengadaan_id($id);
        $this->smarty->assign("result_info", $result);
        //image 
        $filepath = base_url() . 'resource/userfile/pengadaan/';
        $this->smarty->assign("filepath", $filepath);
        // data
        $result = $this->m_pengajuan_barang->get_all_pengajuan_barang_by_pengadaan_id($id);
        $this->smarty->assign("rs_id", $result);
        //get total
        $total = $this->m_pengajuan_barang->get_total_by_pengadaan_id($id);
        $this->smarty->assign("total", $total);

        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    public function delete_process() {
        //cek data
        $this->tnotification->set_rules('pengadaan_id', 'ID Pengadaan', 'trim|required');

        //proses
        if ($this->tnotification->run() !== FALSE) {

            //cek di barang
            $result = $this->m_barang->get_barang_by_pengadaan_id($this->input->post('pengadaan_id'));

            if (empty($result)) {
                //hapus pengajuan barang
                if ($this->m_pengajuan_barang->delete_by_pengadaan_id($this->input->post('pengadaan_id'))) {
                    // notification
                    $this->tnotification->delete_last_field();
                    $this->tnotification->sent_notification("success", "Data barang berhasil dihapus");
                } else {
                    // default error
                    $this->tnotification->sent_notification("error", "Data barang gagal dihapus");
                }

                // insert
                if ($this->m_pengadaan->delete($this->input->post('pengadaan_id'))) {
                    // $last_pengadaan_id = $this->m_pengadaan->get_last_pengadaan_id();
                    //upload
                    if (!empty($_FILES['pengadaan_att_surat']['tmp_name'])) {
                        // load
                        $this->load->library('tupload');
                        // delete
                        $filename = "pengadaan_att_surat_" . $this->input->post('pengadaan_id');
                        $filepath = 'resource/userfile/pengadaan/' . $filename;
                        if (is_file($filepath)) {
                            unlink($filepath);
                        }
                    }
                    // notification
                    $this->tnotification->delete_last_field();
                    $this->tnotification->sent_notification("success", "Data berhasil dihapus");
                } else {
                    // default error
                    $this->tnotification->sent_notification("error", "Data gagal dihapus");
                }
            } else {
                $this->tnotification->sent_notification("error", "Pengadaan tidak dapat dihapus data telah masuk dalam master barang");
                redirect('inventarisasi/pengadaan/delete/' . $this->input->post('pengadaan_id'));
            }
        }
        redirect('inventarisasi/pengadaan/');
    }

    public function add() {
        $this->_set_page_rule("R");

        // set template content
        $this->smarty->assign("template_content", "inventarisasi/pengadaan/add.html");

        $rs_bagian = $this->m_bagian->get_all_bagian();
        $this->smarty->assign("rs_bagian", $rs_bagian);

        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    public function add_process() {
        //cek data
        $this->tnotification->set_rules('pengadaan_nomor', 'Nomor Pengadaan', 'trim|required');
        $this->tnotification->set_rules('pengadaan_date', 'Tanggal Pengadaan', 'trim|required');
        $this->tnotification->set_rules('bagian_cd', 'Bagian', 'trim|required');

        //proses
        if ($this->tnotification->run() !== FALSE) {
            // validasi
            $result = $this->m_pengadaan->get_pengadaan_by_pengadaan_nomor($this->input->post('pengadaan_nomor'));
            if (!empty($result)) {
                // default error
                $this->tnotification->sent_notification("error", "Nomor Pengadaan sudah digunakan, gunakan kode yang lain");
            } else {
                $nomor_pengadaan = $this->input->post('pengadaan_nomor');
                $nomor_pengadaan = str_replace(".", "",$nomor_pengadaan);
                $nomor_pengadaan = str_replace("-", "",$nomor_pengadaan);
                $nomor_pengadaan = str_replace("/", "_",$nomor_pengadaan);
                //upload
                if (!empty($_FILES['pengadaan_att_surat']['tmp_name'])) {
                    // load
                    $this->load->library('tupload');
                    // delete 
                    $filename = $nomor_pengadaan; 
                    $filepath = 'resource/userfile/pengadaan/' . $nomor_pengadaan;
 
                    // upload config
                    $config['upload_path'] = 'resource/userfile/pengadaan/';
                    $config['allowed_types'] = 'gif|jpg|png';
                    $config['max_size'] = '1000';
                    $config['max_width'] = '1024';
                    $config['max_height'] = '800';
                    $config['file_name'] = $filename; 
                    $this->tupload->initialize($config);
                    // process upload images 
                    if(!$this->tupload->do_upload_image('pengadaan_att_surat')){
                        // jika gagal (kembalikan pesan)
                        $this->tnotification->set_error_message($this->tupload->display_errors());
                        $this->tnotification->sent_notification("error", "Foto gagal diupload");
                        $filename = null;
                    } else {
                        $data_upload = $this->tupload->data();
                        $filename = $data_upload['file_name']; 
                        $this->tnotification->sent_notification("success", "Data berhasil disimpan");
                    }
                }
                    
                // parameter 
                $params = array( 
                    'pengadaan_nomor'           => $this->input->post('pengadaan_nomor'),
                    'pengadaan_date'            => $this->input->post('pengadaan_date'),
                    'bagian_cd'                 => $this->input->post('bagian_cd'),
                    'pengadaan_ba_dist'         => null,
                    'pengadaan_tgl_dist'        => null,
                    'pengadaan_keterangan'      => $this->input->post('pengadaan_keterangan'),
                    'pengadaan_att_surat'       => $filename,
                    'pengadaan_att_dist'        => null,
                    'pengadaan_generate'        => 0,
                    'mdb'                       => $this->com_user['user_id'] 
                );
                // insert
                if ($this->m_pengadaan->insert($params)) {
                    $last_pengadaan_id = $this->m_pengadaan->get_last_pengadaan_id(); 
                    redirect('inventarisasi/pengadaan/pengajuan_barang/' . $last_pengadaan_id); 
                    // notification
                    $this->tnotification->delete_last_field();
                    $this->tnotification->sent_notification("success", "Data berhasil disimpan");
                } else {
                    // default error
                    $this->tnotification->sent_notification("error", "Data gagal disimpan");
                }
            }
        } else {
            // default error
            $this->tnotification->sent_notification("error", "Data gagal disimpan");
        }
        redirect('inventarisasi/pengadaan/add');
    }

    public function pengajuan_barang($id) {
        $this->_set_page_rule("R");

        // set template content 
        $this->smarty->assign("template_content", "inventarisasi/pengadaan/pengajuan_barang.html");

        //data
        $result = $this->m_pengadaan->get_pengadaan_by_pengadaan_id($id);
        $this->smarty->assign("result_info", $result);

        //data barang        
        /* start of pagination --------------------- */
        // pagination
        $config['base_url'] = site_url("inventarisasi/pengadaan/pengajuan_barang/$id/");
        $config['total_rows'] = $this->m_pengajuan_barang->get_count_all_pengajuan_barang_by_pengadaan_id($id);
        $config['uri_segment'] = 5;
        $config['per_page'] = 10;
        $this->pagination->initialize($config);
        $pagination['data'] = $this->pagination->create_links();
        // pagination attribute
        $start = $this->uri->segment(5, 0) + 1;
        $end = $this->uri->segment(5, 0) + $config['per_page'];
        $end = (($end > $config['total_rows']) ? $config['total_rows'] : $end);
        $pagination['start'] = ($config['total_rows'] == 0) ? 0 : $start;
        $pagination['end'] = $end;
        $pagination['total'] = $config['total_rows'];
        // pagination assign value
        $this->smarty->assign("pagination", $pagination);
        $this->smarty->assign("no", $start);
        /* end of pagination ---------------------- */

        // get data
        $params = array($id, ($start - 1), $config['per_page']);
        $rs_id = $this->m_pengajuan_barang->get_pengajuan_barang_by_pengadaan_id_pagination($params);
        $this->smarty->assign("rs_id", $rs_id);

        //get total
        $total = $this->m_pengajuan_barang->get_total_by_pengadaan_id($id);
        $this->smarty->assign("total", $total);

        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    public function add_barang($id) {
        $this->_set_page_rule("R");

        // set template content 
        $this->smarty->assign("template_content", "inventarisasi/pengadaan/add_barang.html");

        //data
        $result = $this->m_pengadaan->get_pengadaan_by_pengadaan_id($id);
        $this->smarty->assign("result_info", $result);
        //data
        $rs_kategoribarang = $this->m_kategoribarang->get_all_kategoribarang();
        $this->smarty->assign("rs_kategoribarang", $rs_kategoribarang);
        
        $rs_gpbarang = $this->m_group_barang->get_all_group_barang();
        $this->smarty->assign("rs_gpbarang", $rs_gpbarang);
        
        $rs_jenisbarang = $this->m_jenisbarang->get_all_jenisbarang();
        $this->smarty->assign("rs_jenisbarang", $rs_jenisbarang);
        
        $rs_merk = $this->m_merk->get_all_merk();
        $this->smarty->assign("rs_merk", $rs_merk);
        
        $rs_lokasi = $this->m_lokasi->get_all_lokasi();
        $this->smarty->assign("rs_lokasi", $rs_lokasi);

        $rs_ruang = $this->m_ruang->get_all_ruang();
        $this->smarty->assign("rs_ruang", $rs_ruang);
        
        $rs_vendor = $this->m_vendor->get_all_vendor();
        $this->smarty->assign("rs_vendor", $rs_vendor);

        /*
          $rs_barang_kondisi = Array(0 => Array("name" => "baik"), 1 => Array("name" => "rusak"));
          $this->smarty->assign("rs_barang_kondisi", $rs_barang_kondisi);
          $rs_barang_status = Array(0 => Array("name" => "digunakan"),
          1 => Array("name" => "digudang"),
          2 => Array("name" => "disewakan"),
          3 => Array("name" => "dipinjamkan"),
          4 => Array("name" => "service"));
          $this->smarty->assign("rs_barang_status", $rs_barang_status);
         */
        //get total
        $total = $this->m_pengajuan_barang->get_total_by_pengadaan_id($id);
        $this->smarty->assign("total", $total);

        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    public function add_barang_process() {
        $this->tnotification->set_rules('pengajuanbarang_nama', 'Nama', 'trim|required');
        $this->tnotification->set_rules('pengajuanbarang_qty', 'Jumlah', 'trim|required');
        $this->tnotification->set_rules('pengajuanbarang_hargasatuan', 'Tanggal Pengadaan', 'trim|required');
        $this->tnotification->set_rules('pengajuanbarang_penyusutan', 'Penyusutan', 'trim|required');
        $this->tnotification->set_rules('gpbarang_cd', 'Group Aset', 'trim|required');
        $this->tnotification->set_rules('lokasi_cd', 'Lokasi', 'trim|required');
        $this->tnotification->set_rules('kategoribarang_cd', 'Kategori', 'trim|required');
        // $this->tnotification->set_rules('barang_tglbeli', 'Tanggal Beli', 'trim|required');
        // asset tidak terikat
        if ($this->input->post('gpbarang_cd') == '2') {
            $this->tnotification->set_rules('jenisbarang_cd', 'Jenis', 'trim|required');
            $this->tnotification->set_rules('merk_cd', 'Merk', 'trim|required');
            $this->tnotification->set_rules('ruang_cd', 'Ruang', 'trim|required');
            $this->tnotification->set_rules('vendor_cd', 'Vendor', 'trim|required');
        }
        //proses
        if ($this->tnotification->run() !== FALSE) {
            // parameter  
            $pengajuanbarang_tgl_garansi = ($this->input->post('pengajuanbarang_tgl_garansi') == '') ? null : $this->input->post('pengajuanbarang_tgl_garansi');

            $params = array(
                $this->input->post('pengajuanbarang_nama'),
                str_replace(".", "", $this->input->post('pengajuanbarang_qty')),
                str_replace(".", "", $this->input->post('pengajuanbarang_hargasatuan')),
                $this->input->post('pengajuanbarang_keterangan'),
                $this->input->post('pengajuanbarang_garansi'),
                $pengajuanbarang_tgl_garansi,
                $this->input->post('pengajuanbarang_susut'),
                $this->input->post('pengajuanbarang_penyusutan'),
                $this->input->post('pengajuanbarang_status'),
                $this->input->post('pengajuanbarang_barang_kondisi'),
                $this->input->post('pengajuanbarang_generate_noiventaris'),
                $this->input->post('gpbarang_cd'),
                $this->input->post('vendor_cd'),
                $this->input->post('lokasi_cd'),
                $this->input->post('ruang_cd'),
                $this->input->post('kategoribarang_cd'),
                $this->input->post('jenisbarang_cd'),
                $this->input->post('merk_cd'),
                $this->input->post('pengadaan_id'),
                $this->com_user['user_id']
            );
            // insert
            if ($this->m_pengajuan_barang->insert($params)) {
                // notification
                $this->tnotification->delete_last_field();
                $this->tnotification->sent_notification("success", "Data berhasil disimpan");
            } else {
                // default error
                $this->tnotification->sent_notification("error", "Data gagal disimpan");
            }
        } else {
            // default error
            $this->tnotification->sent_notification("error", "Data gagal disimpan");
        }
        redirect('inventarisasi/pengadaan/add_barang/' . $this->input->post('pengadaan_id'));
    }

    public function edit_barang($id, $pengajuanbarang_id) {
        $this->_set_page_rule("R");

        // set template content 
        $this->smarty->assign("template_content", "inventarisasi/pengadaan/edit_barang.html");

        //data
        $result = $this->m_pengadaan->get_pengadaan_by_pengadaan_id($id);
        $this->smarty->assign("result_info", $result);
        //pengadaan_detail
        //dataedit
        $result = $this->m_pengajuan_barang->get_pengajuan_barang_by_pengajuanbarang_id($pengajuanbarang_id);
        $this->smarty->assign("result", $result);

        //data
        $rs_kategoribarang = $this->m_kategoribarang->get_all_kategoribarang();
        $this->smarty->assign("rs_kategoribarang", $rs_kategoribarang);
        $rs_gpbarang = $this->m_group_barang->get_all_group_barang();
        $this->smarty->assign("rs_gpbarang", $rs_gpbarang);
        $rs_jenisbarang = $this->m_jenisbarang->get_all_jenisbarang();
        $this->smarty->assign("rs_jenisbarang", $rs_jenisbarang);
        $rs_merk = $this->m_merk->get_all_merk();
        $this->smarty->assign("rs_merk", $rs_merk);
        $rs_lokasi = $this->m_lokasi->get_all_lokasi();
        $this->smarty->assign("rs_lokasi", $rs_lokasi);

        $rs_ruang = $this->m_ruang->get_all_ruang();
        $this->smarty->assign("rs_ruang", $rs_ruang);
        $rs_vendor = $this->m_vendor->get_all_vendor();
        $this->smarty->assign("rs_vendor", $rs_vendor);

        //get total
        $total = $this->m_pengajuan_barang->get_total_by_pengadaan_id($id);
        $this->smarty->assign("total", $total);
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    public function edit_barang_process() {
        $this->tnotification->set_rules('pengajuanbarang_nama', 'Nama', 'trim|required');
        $this->tnotification->set_rules('pengajuanbarang_qty', 'Jumlah', 'trim|required');
        $this->tnotification->set_rules('pengajuanbarang_hargasatuan', 'Tanggal Pengadaan', 'trim|required');
        $this->tnotification->set_rules('pengajuanbarang_penyusutan', 'Penyusutan', 'trim|required');
        $this->tnotification->set_rules('gpbarang_cd', 'Group Aset', 'trim|required');
        $this->tnotification->set_rules('lokasi_cd', 'Lokasi', 'trim|required');
        $this->tnotification->set_rules('kategoribarang_cd', 'Kategori', 'trim|required');
        // asset tidak terikat
        if ($this->input->post('gpbarang_cd') == '2') {
            $this->tnotification->set_rules('jenisbarang_cd', 'Jenis', 'trim|required');
            $this->tnotification->set_rules('merk_cd', 'Merk', 'trim|required');
            $this->tnotification->set_rules('ruang_cd', 'Ruang', 'trim|required');
            $this->tnotification->set_rules('vendor_cd', 'Vendor', 'trim|required');
        }
        //proses
        if ($this->tnotification->run() !== FALSE) {
            // parameter 
            $pengajuanbarang_tgl_garansi = ($this->input->post('pengajuanbarang_tgl_garansi') == '') ? null : $this->input->post('pengajuanbarang_tgl_garansi');

            $params = array(
                $this->input->post('pengajuanbarang_nama'),
                $this->input->post('pengajuanbarang_qty'),
                str_replace(".", "", $this->input->post('pengajuanbarang_hargasatuan')),
                $this->input->post('pengajuanbarang_keterangan'),
                $this->input->post('pengajuanbarang_garansi'),
                $pengajuanbarang_tgl_garansi,
                $this->input->post('pengajuanbarang_susut'),
                $this->input->post('pengajuanbarang_penyusutan'),
                $this->input->post('pengajuanbarang_status'),
                $this->input->post('pengajuanbarang_barang_kondisi'),
                $this->input->post('pengajuanbarang_generate_noiventaris'),
                $this->input->post('gpbarang_cd'),
                $this->input->post('vendor_cd'),
                $this->input->post('lokasi_cd'),
                $this->input->post('ruang_cd'),
                $this->input->post('kategoribarang_cd'),
                $this->input->post('jenisbarang_cd'),
                $this->input->post('merk_cd'),
                $this->input->post('pengadaan_id'),
                $this->com_user['user_id'],
                $this->input->post('pengajuanbarang_id')
            );
            // insert
            if ($this->m_pengajuan_barang->edit($params)) {
                // notification
                $this->tnotification->delete_last_field();
                $this->tnotification->sent_notification("success", "Data berhasil disimpan");
            } else {
                // default error
                $this->tnotification->sent_notification("error", "Data gagal disimpan");
            }
        } else {
            // default error
            $this->tnotification->sent_notification("error", "Data gagal disimpan");
        }
        redirect('inventarisasi/pengadaan/edit_barang/' . $this->input->post('pengadaan_id') . '/' . $this->input->post('pengajuanbarang_id'));
    }

    public function delete_barang($id, $data_id) {
        $this->_set_page_rule("R");

        // set template content 
        $this->smarty->assign("template_content", "inventarisasi/pengadaan/delete_barang.html");

       
        //data
        $result = $this->m_pengadaan->get_pengadaan_by_pengadaan_id($id);
        $this->smarty->assign("result_info", $result);

        //dataedit
        $result = $this->m_pengajuan_barang->get_pengajuan_barang_by_pengajuanbarang_id($data_id);
        $this->smarty->assign("result", $result);

        //data
        $rs_kategoribarang = $this->m_kategoribarang->get_all_kategoribarang();
        $this->smarty->assign("rs_kategoribarang", $rs_kategoribarang);
        $rs_gpbarang = $this->m_group_barang->get_all_group_barang();
        $this->smarty->assign("rs_gpbarang", $rs_gpbarang);
        $rs_jenisbarang = $this->m_jenisbarang->get_all_jenisbarang();
        $this->smarty->assign("rs_jenisbarang", $rs_jenisbarang);
        $rs_merk = $this->m_merk->get_all_merk();
        $this->smarty->assign("rs_merk", $rs_merk);
        $rs_lokasi = $this->m_lokasi->get_all_lokasi();
        $this->smarty->assign("rs_lokasi", $rs_lokasi);

        $bulan = array("01" => "Januari",
            "02" => "Februari",
            "03" => "Maret",
            "04" => "April",
            "05" => "Mei",
            "06" => "Juni",
            "07" => "Juli",
            "08" => "Agustus",
            "09" => "September",
            "10" => "Oktober",
            "11" => "November",
            "12" => "Desember");
        $this->smarty->assign("bulan", $bulan);

        //get total
        $total = $this->m_pengajuan_barang->get_total_by_pengadaan_id($id);
        $this->smarty->assign("total", $total);

        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    public function delete_barang_process() {
        $this->tnotification->set_rules('pengajuanbarang_id', 'Pengajuan ID', 'trim|required');
        //proses
        if ($this->tnotification->run() !== FALSE) {
            // validasi
            // parameter
            $params = array(
                $this->input->post('pengajuanbarang_id')
            );
            // insert
            if ($this->m_pengajuan_barang->delete($params)) {
                // notification
                $this->tnotification->delete_last_field();
                $this->tnotification->sent_notification("success", "Data berhasil dihapus");
            } else {
                // default error
                $this->tnotification->sent_notification("error", "Data gagal dihapus");
            }
        } else {
            // default error
            $this->tnotification->sent_notification("error", "Data gagal dihapus");
        }
        redirect('inventarisasi/pengadaan/pengajuan_barang/' . $this->input->post('pengadaan_id') . '/');
    }

    // welcome administrator
    public function view_pengajuan($id) {
        $rs_pengajuan_barang = $this->m_pengajuan_barang->get_all_pengajuan_barang_by_pengadaan_id($id);

        $html = '<tbody>
                <tr>
                    <th width="3%">No</th>
                    <th width="35%">Nama</th> 
                    <th width="5%">Qty</th> 
                    <th width="10%">Perkiraan<br />Harga Satuan<br />(Rp)</th>
                    <th width="15%">Jumlah(Rp)</th> 
                    <th width="10%">Keterangan</th>
                    <th width="10%">Lokasi</th>  
                 </tr>';
        $no = 1;
        if ($rs_pengajuan_barang) {
            foreach ($rs_pengajuan_barang as $row) {
                // echo $row['pengajuanbarang_nama'];
                $total = $row['pengajuanbarang_hargasatuan'] * $row['pengajuanbarang_qty'];
                $html .='<tr>';
                $html .='<td align="center">' . $no++ . '</td>';
                $html .='<td>' . $row['pengajuanbarang_nama'] . '</td>';
                $html .='<td align="center">' . $row['pengajuanbarang_qty'] . '</td>';
                $html .='<td align="right">' . $row['pengajuanbarang_hargasatuan'] . '</td>';
                $html .='<td align="right">' . $total . '</td>';
                $html .='<td>' . $row['pengajuanbarang_keterangan'] . '</td>';
                $html .='<td>' . $row['lokasi_name'] . '</td>';
                $html .='</tr>';
            }
        } else {
            $html .='<tr><td colspan="7">Data kosong.</td></tr>';
        }
        $html .='</tbody>';

        echo $html;
    }

    /*
     * DISTRIBUSI 
     */

    public function distribusi($id) {
        $this->_set_page_rule("R"); 
        // set template content
        $this->smarty->assign("template_content", "inventarisasi/distribusi/list.html"); 

        //data
        $result_info = $this->m_pengadaan->get_pengadaan_by_pengadaan_id($id);
        $this->smarty->assign("result_info", $result_info);     
        
        $rs_ruang = $this->m_ruang->get_all_ruang();
        $this->smarty->assign("rs_ruang", $rs_ruang); 
        //get total
        $total = $this->m_pengajuan_barang->get_total_by_pengadaan_id($id);
        $this->smarty->assign("total", $total);

        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();

        //data barang        
        /* start of pagination --------------------- */
        // pagination
        $config['base_url'] = site_url("inventarisasi/pengadaan/distribusi/$id/");
        $config['total_rows'] = $this->m_pengadaan->get_count_detail_pengajuan_barang_pagination_by_pengadaan_id($id);
        $config['uri_segment'] = 5;
        $config['per_page'] = 30;
        $this->pagination->initialize($config);
        $pagination['data'] = $this->pagination->create_links();
        // pagination attribute
        $start = $this->uri->segment(5, 0) + 1;
        $end = $this->uri->segment(5, 0) + $config['per_page'];
        $end = (($end > $config['total_rows']) ? $config['total_rows'] : $end);
        $pagination['start'] = ($config['total_rows'] == 0) ? 0 : $start;
        $pagination['end'] = $end;
        $pagination['total'] = $config['total_rows'];
        // pagination assign value
        $this->smarty->assign("pagination", $pagination);
        $this->smarty->assign("no", $start);
        /* end of pagination ---------------------- */

        // get data
        $params = array($id, ($start - 1), $config['per_page']);
        $result = $this->m_pengadaan->get_detail_pengajuan_barang_pagination_by_pengadaan_id($params);
        $this->smarty->assign("rs_id", $result);

        $filepath = base_url() . 'resource/userfile/pengadaan/';
        $this->smarty->assign("filepath", $filepath);

        // output
        parent::display();
    }  
    
    public function cetak_excel($id) {
        $this->_set_page_rule("R");
        $this->load->library('phpexcel');
        error_reporting(0);
        set_time_limit(0);

        // get list data
        $results = $this->m_barang->get_barang_by_pengadaan_id($id);
        $result = $this->m_pengadaan->get_pengadaan_by_pengadaan_id($id);

        //excel
        $filepath = "resource/doc/template/berita_acara.xlsx";

        $objReader = PHPExcel_IOFactory::createReader('Excel2007');
        $this->phpexcel = $objReader->load($filepath);
        // set active sheet
        $objWorksheet = $this->phpexcel->setActiveSheetIndex(0);
        $sheet_name = strtoupper('SHEET');
        $objWorksheet->setTitle($sheet_name);

        //tanggal
        $x = mktime(0, 0, 0, date("m"), date("d"), date("Y"));
        $namahari = date("l", $x);
        if ($namahari == "Sunday")
            $namahari = "Minggu";
        else if ($namahari == "Monday")
            $namahari = "Senin";
        else if ($namahari == "Tuesday")
            $namahari = "Selasa";
        else if ($namahari == "Wednesday")
            $namahari = "Rabu";
        else if ($namahari == "Thursday")
            $namahari = "Kamis";
        else if ($namahari == "Friday")
            $namahari = "Jumat";
        else if ($namahari == "Saturday")
            $namahari = "Sabtu";

        $bulan = array("01" => "Jan",
            "02" => "Feb",
            "03" => "Mar",
            "04" => "Apr",
            "05" => "Mei",
            "06" => "Jun",
            "07" => "Jul",
            "08" => "Ags",
            "09" => "Sep",
            "10" => "Okt",
            "11" => "Nov",
            "12" => "Des");

        $tgl = date("d");
        $bln = $bulan[date("m")];
        $thn = date("Y");
        $tanggal = $namahari . ", " . $tgl . "/" . $bln . "/" . $thn;
        $tanggal_full = $tgl . " " . $bln . " " . $thn;

        // header
        $objWorksheet->setCellValue('B4', 'No : ' . $result['pengadaan_ba_dist']);
        $objWorksheet->setCellValue('B7', 'Pada hari ini : ' . $tanggal . ' yang bertanda tangan dibawah ini :');
        $objWorksheet->setCellValue('D9', ': ' . $result['bagian_penanggungjawab']);
        $objWorksheet->setCellValue('D10', ': ' . $result['bagian_name']);

        $styleBorder = array(
            'borders' => array(
                'allborders' => array('style' => PHPExcel_Style_Border::BORDER_THIN)
        ));
        $heading1 = array(
            'font' => array(
                'bold' => false,
                'size' => 9
            )
        );
        $kolom = array('B', 'C', 'D', 'E', 'F');

        // parameters
        $no = 1;
        $start = 15;
        $row = 15;
        foreach ($results as $data) {
            for ($j = 0; $j < 5; $j++) {
                $objWorksheet->getStyle($kolom[$j] . $row)->applyFromArray($styleBorder);
                $objWorksheet->getStyle($kolom[$j] . $row)->applyFromArray($heading1);
            }

            // set values to row
            $objWorksheet->setCellValue('B' . $row, $no);
            $objWorksheet->setCellValue('C' . $row, $data['barang_nama']);
            //$objWorksheet->setCellValue('D' . $row, $data['barang_nama']);
            $objWorksheet->setCellValue('D' . $row, "1");
            $objWorksheet->setCellValue('E' . $row, $data['barang_noinventaris']);
            $objWorksheet->setCellValue('F' . $row, $data['ruang_name']);
            $row++;
            $no++;
        }

        $objWorksheet->setCellValue('B' . ($row + 2), 'Berita acara ini dibuat untuk digunakan sebagaimana mestinya');
        $objWorksheet->setCellValue('E' . ($row + 4), 'Yogyakarta, ' . $tanggal_full);

        $objWorksheet->setCellValue('E' . ($row + 6), 'Bagian yang menerima');
        $objWorksheet->setCellValue('C' . ($row + 6), 'Bagian yang menyerahkan');
        $objWorksheet->setCellValue('C' . ($row + 7), 'Rumah Tangga, ' . $tanggal_full);

        $objWorksheet->setCellValue('C' . ($row + 11), 'R.Muhammad Maskuri, S.Sos I, M.M.');
        $objWorksheet->setCellValue('E' . ($row + 11), $result['bagian_penanggungjawab']);


        // output file
        $file_name = 'Berita Acara Serah Terima Barang - ' . $result['pengadaan_nomor'];
        //--
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename=' . $file_name . '.xlsx');
        header('Cache-Control: max-age=0');
        // output
        $obj_writer = PHPExcel_IOFactory::createWriter($this->phpexcel, 'Excel2007');
        $obj_writer->save('php://output');
    }

    public function cetak_pdf($id) {
        $this->_set_page_rule("R");

        // get list data
        $results = $this->m_barang->get_barang_by_pengadaan_id($id);
        $result = $this->m_pengadaan->get_pengadaan_by_pengadaan_id($id);

        //tanggal
        $x = mktime(0, 0, 0, date("m"), date("d"), date("Y"));
        $namahari = date("l", $x);
        if ($namahari == "Sunday")
            $namahari = "Minggu";
        else if ($namahari == "Monday")
            $namahari = "Senin";
        else if ($namahari == "Tuesday")
            $namahari = "Selasa";
        else if ($namahari == "Wednesday")
            $namahari = "Rabu";
        else if ($namahari == "Thursday")
            $namahari = "Kamis";
        else if ($namahari == "Friday")
            $namahari = "Jumat";
        else if ($namahari == "Saturday")
            $namahari = "Sabtu";

        $bulan = array("01" => "Jan",
            "02" => "Feb",
            "03" => "Mar",
            "04" => "Apr",
            "05" => "Mei",
            "06" => "Jun",
            "07" => "Jul",
            "08" => "Ags",
            "09" => "Sep",
            "10" => "Okt",
            "11" => "Nov",
            "12" => "Des");

        $tgl = date("d");
        $bln = $bulan[date("m")];
        $thn = date("Y");
        $tanggal = $namahari . ", " . $tgl . "/" . $bln . "/" . $thn;
        $tanggal_full = $tgl . " " . $bln . " " . $thn;


        // load library
        $this->load->library('tcpdf');
        // data
        // page setting
        $this->tcpdf->SetPrintHeader(false);
        $this->tcpdf->SetPrintFooter(false);
        $this->tcpdf->SetDisplayMode('real');
        $this->tcpdf->SetMargins(30, 30, 30, false);
        $this->tcpdf->SetFont('times', '', 9, '');
        $this->tcpdf->AddPage('P', 'A4');

        // html content
        $html = '';
        $html .= '<html>';
        $html .= '<table width = "100%" border="0">';
        $html .= '  <tr>';
        $html .= '      <td align="center" colspan="2"><b>Berita Acara Serah Terima Barang</b></td>';
        $html .= '  </tr>';
        $html .= '  <tr>';
        $html .= '      <td align="center" colspan="2">' . 'No : ' . $result['pengadaan_ba_dist'] . '<br/></td>';
        $html .= '  </tr>';

        $html .= '  <tr>';
        $html .= '      <td align="left" colspan="2">' . 'Pada hari ini : ' . $tanggal . ' yang bertanda tangan dibawah ini :' . '</td>';
        $html .= '  </tr>';
        $html .= '  <tr>';
        $html .= '      <td width="5%"></td>';
        $html .= '      <td width="95%" align="left">' . "Nama  &nbsp; : " . $result['bagian_penanggungjawab'] . '</td>';
        $html .= '  </tr>';
        $html .= '  <tr>';
        $html .= '      <td width="5%" ></td>';
        $html .= '      <td width="95%" align="left">' . 'Bagian : ' . $result['bagian_name'] . '</td>';
        $html .= '  </tr>';
        $html .= '  <tr>';
        $html .= '      <td align="left" colspan="2"><br /><br />Telah Terima barang dari bagian rumah tangga dengan spesifikasi sebagai berikut : <br /></td>';
        $html .= '  </tr>';
        $html .= '</table>';

        //$html = '<html>';
        $html .= '<table border="1" cellpadding="0" width = "100%" cellspacing="0" style="font-size:9;">';
        $html .= '  <tr style="background-color:#D7DADE; font-weight:bold;">';
        $html .= '      <th width="5%" align="center">No</th>
                        <th width="35%" align="center">Nama Barang</th>
                        <th width="10%" align="center">Jumlah</th>
                        <th width="30%" align="center">Kode Inventaris</th>
                        <th width="20%" align="center">Lokasi</th>';
        $html .= '  </tr>';

        $no = 1;
        foreach ($results as $data) {
            set_time_limit(0);
            $html .= '  <tr>';
            $html .= '      <td align="center">' . $no . '. </td>';
            $html .= '      <td>' . $data['barang_nama'] . '</td>';
            // $html .= '      <td>' . $data['barang_nama'] . '</td>';
            $html .= '      <td align="center">' . "1" . '</td>';
            $html .= '      <td>' . $data['barang_noinventaris'] . '</td>';
            $html .= '      <td>' . $data['ruang_name'] . '</td>';
            $html .= '  </tr>';
            $no = $no + 1;
        }

        $html .= '</table>';

        //footer
        $html .= '<table width = "100%" border="0">';
        $html .= '  <tr>';
        $html .= '      <td align="left" colspan="5"><br /><br />Berita acara ini dibuat untuk digunakan sebagaimana mestinya<br /></td>';
        $html .= '  </tr>';
        $html .= '  <tr>';
        $html .= '      <td width="10%"></td>';
        $html .= '      <td width="35%"></td>';
        $html .= '      <td width="20%" ></td>';
        $html .= '      <td width="35%" align="left">' . 'Yogyakarta, ' . $tanggal_full . '</td>';
        $html .= '  </tr>';
        $html .= '  <tr>';
        $html .= '      <td width="10%" ></td>';
        $html .= '      <td width="35%" align="left">Bagian yang menyerahkan</td>';
        $html .= '      <td ></td>';
        $html .= '      <td width="35%" align="left">Bagian yang menerima</td>';
        $html .= '  </tr>';
        $html .= '  <tr>';
        $html .= '      <td ></td>';
        $html .= '      <td align="left">Rumah Tangga, ' . $tanggal_full . '</td>';
        $html .= '      <td></td>';
        $html .= '      <td></td>';
        $html .= '  </tr>';
        $html .= '  <tr>';
        $html .= '      <td><br/><br/></td>';
        $html .= '      <td></td>';
        $html .= '      <td></td>';
        $html .= '      <td></td>';
        $html .= '  </tr>';
        $html .= '  <tr>';
        $html .= '      <td ></td>';
        $html .= '      <td align="left">R.Muhammad Maskuri, S.Sos I, M.M.</td>';
        $html .= '      <td></td>';
        $html .= '      <td>' . $result['bagian_penanggungjawab'] . '</td>';
        $html .= '  </tr>';
        $html .= '</table>';

        $html .= '</html>';

        //echo $html;
        // write
        $this->tcpdf->writeHTML($html, true, false, true, false, 'D');
        // download output
        $this->tcpdf->Output('Berita Acara Serah Terima Barang - ' . $result['pengadaan_nomor'] . '.pdf', 'D');
    }

    /*
     * START AJAX
     */

    public function get_group_asset_by_kategori() {
        $kategoribarang = $this->input->post('kategoribarang');
        $result = $this->m_kategoribarang->get_kategoribarang_by_id($kategoribarang);

        $html = '';
        if (!empty($result)) {
            $html = $result['gpbarang_cd'];
        }

        echo $html;
    }

    public function cari_jenis_barang() {
        $kategoribarang = $this->input->post('kategoribarang');
        $jenisbarang = $this->input->post('jenisbarang');

        $result = $this->m_jenisbarang->get_jenisbarang_by_kategoribarang($kategoribarang);
        $html = '<option></option>';
        foreach ($result as $key => $value) {
            $selected = "";
            if ($jenisbarang == $value['jenisbarang_cd']){
                $selected = 'selected="selected"';
            }
            $html .= '<option value="' . $value['jenisbarang_cd'] . '" '.$selected.'>' . $value['jenisbarang_cd'].' - '.$value['jenisbarang_name'] . '</option>';
        }
        echo $html;
    }

    public function cari_merk_barang() {
        $kategoribarang = $this->input->post('kategoribarang');
        $merk_cd = $this->input->post('merk_cd');

        $result = $this->m_merk->get_merk_by_kategoribarang($kategoribarang);
        $html = '<option></option>';
        foreach ($result as $key => $value) {
            $selected = "";
            if ($merk_cd == $value['merk_cd']){
                $selected = 'selected="selected"';
            }
            $html .= '<option value="' . $value['merk_cd'] . '" '.$selected.'>' . $value['merk_cd'].' - '.$value['merk_name'] . '</option>';
        }
        echo $html;
    }

    /*
     * END AJAX
     */
}
